package cn.edu.niit.server.service.impl;

import cn.edu.niit.server.domain.UserAccount;
import cn.edu.niit.server.repository.UserAccountRepository;
import cn.edu.niit.server.service.UserAccountService;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link UserAccount}.
 */
@Service
@Transactional
public class UserAccountServiceImpl implements UserAccountService {

    private final Logger log = LoggerFactory.getLogger(UserAccountServiceImpl.class);

    private final UserAccountRepository userAccountRepository;

    public UserAccountServiceImpl(UserAccountRepository userAccountRepository) {
        this.userAccountRepository = userAccountRepository;
    }

    @Override
    public UserAccount save(UserAccount userAccount) {
        log.debug("Request to save UserAccount : {}", userAccount);
        return userAccountRepository.save(userAccount);
    }

    @Override
    public Optional<UserAccount> partialUpdate(UserAccount userAccount) {
        log.debug("Request to partially update UserAccount : {}", userAccount);

        return userAccountRepository
            .findById(userAccount.getId())
            .map(
                existingUserAccount -> {
                    if (userAccount.getHead() != null) {
                        existingUserAccount.setHead(userAccount.getHead());
                    }
                    if (userAccount.getNickname() != null) {
                        existingUserAccount.setNickname(userAccount.getNickname());
                    }
                    if (userAccount.getBirthday() != null) {
                        existingUserAccount.setBirthday(userAccount.getBirthday());
                    }
                    if (userAccount.getLogin() != null) {
                        existingUserAccount.setLogin(userAccount.getLogin());
                    }
                    if (userAccount.getIntroduction() != null) {
                        existingUserAccount.setIntroduction(userAccount.getIntroduction());
                    }

                    return existingUserAccount;
                }
            )
            .map(userAccountRepository::save);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<UserAccount> findAll(Pageable pageable) {
        log.debug("Request to get all UserAccounts");
        return userAccountRepository.findAll(pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<UserAccount> findOne(Long id) {
        log.debug("Request to get UserAccount : {}", id);
        return userAccountRepository.findById(id);
    }

    @Override
    public void delete(Long id) {
        log.debug("Request to delete UserAccount : {}", id);
        userAccountRepository.deleteById(id);
    }
}
